//
//  COPickerView.swift
//  CocoProject
//
//  Created by Vill on 2023/11/16.
//

import UIKit

class COPickerView: UIView,UIPickerViewDelegate, UIPickerViewDataSource {

    var pickerSelectClosure: ((_ title: String) -> Void)?
    
    private let background = UIView()
    private let container = UIImageView()
    private let pickerView = UIPickerView()
    private var dataSource: [String]
    
    init(frame: CGRect, dataSource: [String]) {
        self.dataSource = dataSource
        super.init(frame: frame)
        setupSubviews()
        pickerView.selectRow(self.dataSource.count/2, inComponent: 0, animated: false)

    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func draw(_ rect: CGRect) {
        super.draw(rect)
        container.clipsCorners(corners: [.topLeft,.topRight], radius: PTW(20))
    }
    
    private func setupSubviews() {
        background.frame = self.bounds
        background.backgroundColor = .black
        background.alpha = 0
        addSubview(background)
        background.addTap { [weak self] ges in
            guard let `self` = self else { return }
            self.dismiss()
        }
        container.frame = CGRect(x: 0, y: self.height, width: SCREENWIDTH, height: PTW(300))
        container.backgroundColor = UIColor.white
        container.isUserInteractionEnabled = true
        addSubview(container)
        
        let cancel = UIButton(type: .custom)
        cancel.setTitle("Cancel", for: .normal)
        cancel.titleLabel?.font = UIFont.pingfangFont(ofSize: PTW(15), weight: .medium)
        cancel.frame = CGRect(x: PTW(20), y: PTW(10), width: PTW(60), height: PTW(44))
        cancel.setTitleColor(UIColor.colorWithHex("C7C9C9"), for: .normal)
        cancel.addTarget(self, action: #selector(cancelAction), for: .touchUpInside)
        container.addSubview(cancel)
        
        let certain = UIButton(type: .custom)
        certain.setTitle("Done", for: .normal)
        certain.titleLabel?.font = UIFont.pingfangFont(ofSize: PTW(15), weight: .medium)
        certain.frame = CGRect(x: container.width - PTW(80), y:  PTW(10), width:  PTW(60), height:  PTW(44))
        certain.setTitleColor(UIColor.colorWithHex("#1F0043"), for: .normal)
        certain.addTarget(self, action: #selector(doneAction), for: .touchUpInside)
        container.addSubview(certain)
        
        pickerView.dataSource = self
        pickerView.delegate = self
        pickerView.set(left: (SCREENWIDTH  - pickerView.width) / 2)
        pickerView.set(top: (container.height - pickerView.height) / 2)
        container.addSubview(pickerView)
    }
    
    func showIn(view: UIView) {
        view.addSubview(self)
        UIView.animate(withDuration: 0.2) {
            self.container.set(top: self.height - PTW(300))
            self.background.alpha = 0.4
        }
    }
    
    func dismiss() {
        UIView.animate(withDuration: 0.2) {
            self.container.set(top: self.height)
            self.background.alpha = 0
        } completion: { (_) in
            self.removeFromSuperview()
        }
    }
    
    @objc private func cancelAction() {
        dismiss()
    }
    
    @objc private func doneAction() {
        dismiss()
        let index = pickerView.selectedRow(inComponent: 0)
        if let closure = pickerSelectClosure {
            closure(dataSource[index])
        }
    }
    
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }
    
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return dataSource.count
    }
    
    func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
        let attrString = NSMutableAttributedString(string: dataSource[row])
        let attr: [NSAttributedString.Key : Any] = [.foregroundColor: UIColor.colorWithHex("#1F0043"),.font : UIFont.pingfangFont(ofSize: PTW(18), weight: .medium)]
        attrString.addAttributes(attr, range: NSRange(location: 0, length: dataSource[row].count))
        return attrString
    }

}
